<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2020/11/13
 * Time: 10:49
 */

namespace app\api\controller\v20;

use app\common\lib\Util;
use app\common\model\Slide;

/**
 * 启动页
 * @method GET
 * @description 添加隐私政策更新
 * @support v2.4.5
 * Class StartupPage
 * @package app\api\controller\v20
 */
class StartupPage
{
    /**
     * app启动页
     */
    public function index()
    {
        if (!request()->isGet())
        {
            return show(config('code.error'), '未知操作', [], 403);
        }
        $width = input('width/d');
        $height = input('height/d');

        $scale = $height / $width;

        $data = (new Slide())->getStartPageVersion();

        if (!$data['hg'] && Util::AppIsPassShowV1())
        {
            $data = [
                'id' => '',
                'name' => '',
                'description' => '',
                'link' => '',
                'target' => '',
                'image' => '',
                'image2' => '',
                'photo' => '',
            ];
        }

        $arr = [
            'id' => $data['id'] ?? '',
            'name' => $data['name'],
            'description' => $data['description'],
            'target' => $data['target'],
            'link' => htmlspecialchars_decode($data['link']),
            'image' => $data['image']
        ];

        // 后端所有的尺寸
        $photoList = [];

        if (isset($data['photo']) && !empty($data['photo']))
        {
            $photos = unserialize($data['photo']);

            foreach ($photos as $k => $photo)
            {
                $temp1 = explode('|', $photo);

                $url = $temp1[0];
                $photoScale = 99;

                if (count($temp1) === 2)
                {
                    $temp2 = explode('*', $temp1[1]);

                    if (count($temp2) === 2 && $temp2[0] != 0)
                    {
                        $photoScale = $temp2[1] / $temp2[0];
                    }
                }
                // 计算屏幕尺寸差值
                $photoList[] = [
                    'url' => htmlspecialchars_decode($url),
                    'scale' => abs($photoScale - $scale),
                ];
            }
        }

        if (!empty($photoList))
        {
            // 重新排序
            $photoList = resort($photoList, 'scale', 'ASC');

            $arr['image'] = $photoList[0]['url'];
        } else
        {
            if ($scale >= 1.9)
            {
                $arr['image'] = $data['image2'];
            }
        }

        /**
         * 隐私政策版本对应code码
         * @version 2.4.5 code=1
         * @version 2.5.0 code=2
         * @version 2.7.0 code=3
         * @version 2.9.0 code=4
         */
        $updateCode = 4;

        $arr['privacy'] = [
            'title' => '用户及隐私协议更新提示',
            'content' => '感谢您使用并信任准了APP！我们依照相关的法律法规及政策规定，更新并完善了[service]、[privacy]，在此特意向您告知。请您在使用前务必仔细阅读并透彻理解，您同意并接受全部条款后即可正常使用我们的产品/服务。
                
本次我们就《准了隐私政策》的以下协议内容进行了更新完善：准了第三方服务商目录更新、我们如何保护及保存您的个人信息、准了APP权限获取说明等内容。',
            'code' => $updateCode
        ];

        return show(config('code.success'), '加载成功', $arr);
    }
}
